#include <stdio.h>
#include <iostream>
#include <assert.h>
typedef int StackType;

typedef struct Stack
{
    StackType* a;
    int top;
    int capacity;
}Stack;


void StackInit(Stack* pst)
{
    assert(pst);
    pst->a = (StackType*)malloc(sizeof(StackType) * 4);
    pst->capacity = 4;
    pst->top = 0;
}

void StackDestory(Stack* pst)
{
    assert(pst);
    free(pst->a);
    pst->a = nullptr;
    pst->top = pst->capacity = 0;
}

void StackPush(Stack* pst, StackType x)
{
    assert(pst);
    if(pst->capacity == pst->top)
    {
        //栈满;
        StackType* tmp = (StackType*)realloc(pst->a, sizeof(StackType)*pst->capacity * 2);
        if(tmp == nullptr)
        {
            printf("malloc fail");
            exit(-1);
        }
        pst->a = tmp;
        pst->capacity *= 2;
    }
    pst->a[pst->top] = x;
    pst->top++;
}

bool StackEmpty(Stack* pst)
{
    assert(pst);
    return pst->top == 0;
}

void StackPop(Stack* pst)
{
    assert(pst);
    assert(!StackEmpty(pst));

    pst->top--;
}


int StackSize(Stack* pst)
{
    assert(pst);
    return pst->top;
}